﻿// ***********************************************************************
// Assembly         : TSharp.Core
// Author           : tangjingbo
// Created          : 04-10-2013
//
// Last Modified By : tangjingbo
// Last Modified On : 04-11-2013
// ***********************************************************************
// <copyright file="GenericRepository.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using TSharp.Core;
using TSharp.Core.Repository;
[assembly: TSharp.Core.Osgi.RegService(typeof(IGenericRepository<>), typeof(GenericRepository<>))]
namespace TSharp.Core.Repository
{
    /// <summary>
    /// Generic repository wraps given <see cref="IUnitOfWork"/> implementation
    /// and provides unified access to the entities stored in underlying data storage.
    /// </summary>
    /// <remarks>
    /// Additionally to <see cref="IUnitOfWork"/>, the repository supports 
    /// fluently initialized specifications. See also  Specify&lt;TSpecification&gt;() method.
    /// 
    /// All commands have to be executed over started unit of work session.
    ///
    /// Flushing of entities to underlying data storage is in competence of 
    /// given unit of work. In other words, synchronization between in-memory repository
    /// and data storage (e.g. database) is done via unit of work. This way the client
    /// has complete control over calling data storage and can optimize the way the entities
    /// are managed.
    /// </remarks>
    public class GenericRepository<TEntity> :
        ReadonlyRepository<TEntity>, IGenericRepository<TEntity>
        where TEntity : class
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericRepository&lt;TEntity&gt;"/> class.
        /// </summary>
        public GenericRepository()
            : base(AopContext.BeginRequestUnitOfWork() as IUnitOfWork)
        {

        }

        /// <summary>
        /// Inserts entity to the repository.
        /// </summary>
        public virtual void Insert(TEntity entity)
        {
            UnitOfWork.Insert<TEntity>(entity);
        }

        /// <summary>
        /// Updates entity in the repository.
        /// </summary>
        public virtual void Update(TEntity entity)
        {
            UnitOfWork.Update<TEntity>(entity);
        }

        /// <summary>
        /// Deletes entity from the repository.
        /// </summary>
        public virtual void Delete(TEntity entity)
        {
            UnitOfWork.Delete<TEntity>(entity);
        }


    }
}
